package com.sg.service.biz.framework.impl;

import cn.hutool.core.bean.BeanUtil;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.common.util.RedisUtil;
import com.sg.dto.base.framework.req.QueryTagDetailReq;
import com.sg.dto.base.framework.req.QueryTagListReq;
import com.sg.dto.biz.framework.req.*;
import com.sg.dto.biz.framework.res.*;
import com.sg.service.base.framework.MTagService;
import com.sg.common.exception.Assert;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class TagServiceImpl
        implements com.wicket.okrframework.biz.service.TagService {

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private MTagService mTagService;

    /**
     * D1-1查标签列表[616]
     * gen by moon at 12/13/2022, 3:57:27 AM
     */
    @Trace(operationName = "D1-1查标签列表")
    @Tags({@org.apache.skywalking.apm.toolkit.trace.Tag(key = "参数", value = "arg[0]"),
            @org.apache.skywalking.apm.toolkit.trace.Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QueryTagListRespDto queryTagList(QueryTagListReqDto reqDto) {


        List<Tag> listTag_1 = new ArrayList<>();
//步骤0: 1-1-16查询标签列表 - queryTagList
        List<Tag> listTag = new ArrayList<>();
        QueryTagListReq queryTagListReq = new QueryTagListReq();
        queryTagListReq.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID//sourceId:510376_1
        queryTagListReq.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID//sourceId:510378_1
        if (reqDto != null) {
            queryTagListReq.setTagName(reqDto.getTagName());//SimpleFieldAssign//sourceId:26740_1
            queryTagListReq.setTagSource(reqDto.getTagSource());//SimpleFieldAssign//sourceId:26741_1
            queryTagListReq.setIsRecommend(reqDto.getIsRecommend());//SimpleFieldAssign//sourceId:89699_1
            queryTagListReq.setBizAppCode(reqDto.getBizAppCode());//SimpleFieldAssign//sourceId:388705_1
            queryTagListReq.setBusinessSceneCode(reqDto.getBusinessSceneCode());//SimpleFieldAssign//sourceId:89700_1
            queryTagListReq.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:26742_1
            queryTagListReq.setIsArchive(reqDto.getIsArchive());//SimpleFieldAssign//sourceId:26743_1
        }

        /*1-1-16查询标签列表[112]   */
        Assert.isNull(queryTagListReq.getSpaceId(), "D1-1查标签列表-1-1-16查询标签列表-创建于空间ID不能为空", false);
        Assert.isNull(queryTagListReq.getAppId(), "D1-1查标签列表-1-1-16查询标签列表-创建于联盟应用ID不能为空", false);
        listTag = mTagService.queryTagList(queryTagListReq);


        listTag_1 = listTag;

        QueryTagListRespDto retData = new QueryTagListRespDto();
        retData.setTagList(listTag_1.stream().map(item -> BeanUtil.toBean(item, TagDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:34825_1


        return retData;
    }

    /**
     * D1-1修改标签[619]
     * gen by moon at 10/24/2022, 9:43:58 PM
     */
    @Trace(operationName = "D1-1修改标签")
    @Tags({@org.apache.skywalking.apm.toolkit.trace.Tag(key = "参数", value = "arg[0]"),
            @org.apache.skywalking.apm.toolkit.trace.Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public UpdateTagRespDto updateTag(UpdateTagReqDto reqDto) {


        //步骤0: 1-1-16修改标签 - updateTag
        boolean bOOLEAN;
        Tag tag = new Tag();
        if (reqDto != null) {
            tag.setTagId(reqDto.getTagId());//SimpleFieldAssign//sourceId:34739_1
            tag.setTagName(reqDto.getTagName());//SimpleFieldAssign//sourceId:34740_1
            tag.setTagSource(reqDto.getTagSource());//SimpleFieldAssign//sourceId:45906_1
            tag.setIsRecommend(reqDto.getIsRecommend());//SimpleFieldAssign//sourceId:349983_1
            tag.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:34742_1
        }

        /*1-1-16修改标签[110]   */
        Assert.isNull(tag.getTagId(), "D1-1修改标签-1-1-16修改标签-标签ID不能为空", false);
        bOOLEAN = mTagService.updateTag(tag);


        UpdateTagRespDto retData = new UpdateTagRespDto();


        return retData;
    }

    /**
     * D1-1新增标签[618]
     * gen by moon at 10/24/2022, 9:43:57 PM
     */
    @Trace(operationName = "D1-1新增标签")
    @Tags({@org.apache.skywalking.apm.toolkit.trace.Tag(key = "参数", value = "arg[0]"),
            @org.apache.skywalking.apm.toolkit.trace.Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AddTagRespDto addTag(AddTagReqDto reqDto) {


        String string_1 = null;
//步骤0: 1-1-16新增标签 - addTag
        String string = null;
        Tag tag = new Tag();
        if (reqDto != null) {
            tag.setTagName(reqDto.getTagName());//SimpleFieldAssign//sourceId:35266_1
            tag.setTagSource(reqDto.getTagSource());//SimpleFieldAssign//sourceId:34784_1
            tag.setIsRecommend(reqDto.getIsRecommend());//SimpleFieldAssign//sourceId:349973_1
            tag.setBizAppCode(reqDto.getBizAppCode());//SimpleFieldAssign//sourceId:388698_1
            tag.setBusinessSceneCode(reqDto.getBusinessSceneCode());//SimpleFieldAssign//sourceId:349974_1
            tag.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:34785_1
        }

        /*1-1-16新增标签[109]   */
        Assert.isNull(tag.getTagName(), "D1-1新增标签-1-1-16新增标签-标签名称不能为空", false);
        Assert.isNull(tag.getTagSource(), "D1-1新增标签-1-1-16新增标签-标签来源方式不能为空", false);
        Assert.isNull(tag.getIsRecommend(), "D1-1新增标签-1-1-16新增标签-是否推荐不能为空", false);
        Assert.isNull(tag.getSubjectLifeCycle(), "D1-1新增标签-1-1-16新增标签-主体生命周期不能为空", false);
        string = mTagService.addTag(tag);


        string_1 = string;

        AddTagRespDto retData = new AddTagRespDto();
        if (string_1 != null) {
            retData.setTagId(string_1);//SimpleFieldAssign//sourceId:34790_1
        }


        return retData;
    }

    /**
     * D1-1查询标签详情[617]
     * gen by moon at 10/27/2022, 11:00:28 PM
     */
    @Trace(operationName = "D1-1查询标签详情")
    @Tags({@org.apache.skywalking.apm.toolkit.trace.Tag(key = "参数", value = "arg[0]"),
            @org.apache.skywalking.apm.toolkit.trace.Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QueryTagDetailRespDto queryTagDetail(QueryTagDetailReqDto reqDto) {


        Tag tag_1 = null;
//步骤0: 1-1-16查询标签详情 - queryTagDetail
        Tag tag = null;
        QueryTagDetailReq queryTagDetailReq = new QueryTagDetailReq();
        if (reqDto != null) {
            queryTagDetailReq.setTagId(reqDto.getTagId());//SimpleFieldAssign//sourceId:26723_1
            queryTagDetailReq.setTagSource(reqDto.getTagSource());//SimpleFieldAssign//sourceId:34843_1
            queryTagDetailReq.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:34844_1
            queryTagDetailReq.setIsArchive(reqDto.getIsArchive());//SimpleFieldAssign//sourceId:34845_1
        }

        /*1-1-16查询标签详情[203]   */

        tag = mTagService.queryTagDetail(queryTagDetailReq);


        tag_1 = tag;

        QueryTagDetailRespDto retData = new QueryTagDetailRespDto();
        if (tag_1 != null) {
            retData.setTagId(tag_1.getTagId());//SimpleFieldAssign//sourceId:26732_1
            retData.setTagName(tag_1.getTagName());//SimpleFieldAssign//sourceId:26733_1
            retData.setTagSource(tag_1.getTagSource());//SimpleFieldAssign//sourceId:26734_1
            retData.setBizAppCode(tag_1.getBizAppCode());//SimpleFieldAssign//sourceId:406136_1
            retData.setBusinessSceneCode(tag_1.getBusinessSceneCode());//SimpleFieldAssign//sourceId:349967_1
            retData.setIsRecommend(tag_1.getIsRecommend());//SimpleFieldAssign//sourceId:349968_1
            retData.setSubjectLifeCycle(tag_1.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:26735_1
            retData.setOperationInductionId(tag_1.getOperationInductionId());//SimpleFieldAssign//sourceId:26737_1
            retData.setOperateTime(tag_1.getOperateTime());//SimpleFieldAssign//sourceId:26738_1
        }


        return retData;
    }

    /**
     * D1-1新增对象标签(公共)[2757]
     * gen by moon at 11/5/2022, 9:50:08 PM
     */
    @Trace(operationName = "D1-1新增对象标签(公共)")
    @Tags({@org.apache.skywalking.apm.toolkit.trace.Tag(key = "参数", value = "arg[0]"),
            @org.apache.skywalking.apm.toolkit.trace.Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AddObjectTagComRespDto addObjectTagCom(AddObjectTagComReqDto reqDto) {


        Tag tag_1 = null;
        Tag tag_4 = null;
//virtualUsage 1-1-16查询标签详情  17264
        Tag tag = null;
        QueryTagDetailReq queryTagDetailReq = new QueryTagDetailReq();
        queryTagDetailReq.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:419643_1_17264
        queryTagDetailReq.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID//sourceId:349210_1_17264
        if (reqDto != null) {
            queryTagDetailReq.setTagName(reqDto.getTagName());//SimpleFieldAssign//sourceId:86975_1_17264
            queryTagDetailReq.setBizAppCode(reqDto.getBizAppCode());//SimpleFieldAssign//sourceId:419642_1_17264
            queryTagDetailReq.setBusinessSceneCode(reqDto.getBusinessSceneCode());//SimpleFieldAssign//sourceId:419640_1_17264
        }

        /*1-1-16查询标签详情[203]   */
        Assert.isNull(queryTagDetailReq.getTagName(), "D1-1新增对象标签(公共)-1-1-16查询标签详情-标签名称不能为空", false);
        Assert.isNull(queryTagDetailReq.getBizAppCode(), "D1-1新增对象标签(公共)-1-1-16查询标签详情-归属业务应用标识不能为空", false);
        Assert.isNull(queryTagDetailReq.getBusinessSceneCode(), "D1-1新增对象标签(公共)-1-1-16查询标签详情-归属业务场景标识不能为空", false);
        Assert.isNull(queryTagDetailReq.getSubjectLifeCycle(), "D1-1新增对象标签(公共)-1-1-16查询标签详情-主体生命周期不能为空", false);
        Assert.isNull(queryTagDetailReq.getSpaceId(), "D1-1新增对象标签(公共)-1-1-16查询标签详情-创建于空间ID不能为空", false);
        tag = mTagService.queryTagDetail(queryTagDetailReq);


        tag_1 = tag;
        if ((tag == null)) {
            //if(1-1-16查询标签详情.出参 值等于空 )  17265

            String string = null;
            Tag tag_2 = new Tag();
            tag_2.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:352237_1_17266
            if (reqDto != null) {
                tag_2.setTagName(reqDto.getTagName());//SimpleFieldAssign//sourceId:86977_1_17266
                tag_2.setTagSource(reqDto.getTagSource());//SimpleFieldAssign//sourceId:86978_1_17266
                tag_2.setIsRecommend(reqDto.getIsRecommend());//SimpleFieldAssign//sourceId:89708_1_17266
                tag_2.setBizAppCode(reqDto.getBizAppCode());//SimpleFieldAssign//sourceId:388712_1_17266
                tag_2.setBusinessSceneCode(reqDto.getBusinessSceneCode());//SimpleFieldAssign//sourceId:89709_1_17266
            }

            /*1-1-16新增标签[109]   */
            Assert.isNull(tag_2.getTagName(), "D1-1新增对象标签(公共)-1-1-16新增标签-标签名称不能为空", false);
            Assert.isNull(tag_2.getTagSource(), "D1-1新增对象标签(公共)-1-1-16新增标签-标签来源方式不能为空", false);
            Assert.isNull(tag_2.getBizAppCode(), "D1-1新增对象标签(公共)-1-1-16新增标签-归属业务应用标识不能为空", false);
            Assert.isNull(tag_2.getBusinessSceneCode(), "D1-1新增对象标签(公共)-1-1-16新增标签-归属业务场景标识不能为空", false);
            Assert.isNull(tag_2.getSubjectLifeCycle(), "D1-1新增对象标签(公共)-1-1-16新增标签-主体生命周期不能为空", false);
            string = mTagService.addTag(tag_2);


            Tag tag_3 = null;
            if (string != null) {
                QueryTagDetailReq queryTagDetailReq_1 = new QueryTagDetailReq();
                queryTagDetailReq_1.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:419639_1_17270
                queryTagDetailReq_1.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID//sourceId:349212_1_17270
                if (string != null) {
                    queryTagDetailReq_1.setTagId(string);//SimpleFieldAssign//sourceId:349211_1_17270
                }
                if (reqDto != null) {
                    queryTagDetailReq_1.setBizAppCode(reqDto.getBizAppCode());//SimpleFieldAssign//sourceId:419644_1_17270
                    queryTagDetailReq_1.setBusinessSceneCode(reqDto.getBusinessSceneCode());//SimpleFieldAssign//sourceId:419638_1_17270
                }

                /*1-1-16查询新增的标签[203]   */
                Assert.isNull(queryTagDetailReq_1.getTagId(), "D1-1新增对象标签(公共)-1-1-16查询新增的标签-标签ID不能为空", false);
                Assert.isNull(queryTagDetailReq_1.getBizAppCode(), "D1-1新增对象标签(公共)-1-1-16查询新增的标签-归属业务应用标识不能为空", false);
                Assert.isNull(queryTagDetailReq_1.getBusinessSceneCode(), "D1-1新增对象标签(公共)-1-1-16查询新增的标签-归属业务场景标识不能为空", false);
                Assert.isNull(queryTagDetailReq_1.getSubjectLifeCycle(), "D1-1新增对象标签(公共)-1-1-16查询新增的标签-主体生命周期不能为空", false);
                Assert.isNull(queryTagDetailReq_1.getSpaceId(), "D1-1新增对象标签(公共)-1-1-16查询新增的标签-创建于空间ID不能为空", false);
                tag_3 = mTagService.queryTagDetail(queryTagDetailReq_1);


                tag_4 = tag_3;
            }
        }
//processBranchName:正常结束 ,processBranchId:21097

        AddObjectTagComRespDto retData = new AddObjectTagComRespDto();
        if (tag_1 != null) {
            retData.setTagId(tag_1.getTagId());//SimpleFieldAssign//sourceId:87003_1
            retData.setTagName(tag_1.getTagName());//SimpleFieldAssign//sourceId:87004_1
        }
        if (tag_4 != null) {
            retData.setTagId(tag_4.getTagId());//SimpleFieldAssign//sourceId:87003_1
            retData.setTagName(tag_4.getTagName());//SimpleFieldAssign//sourceId:87004_1
        }


        return retData;
    }

    /**
     * D1-1修改对象标签(公共)[4992]
     * gen by moon at 10/24/2022, 9:43:41 PM
     */
    @Trace(operationName = "D1-1修改对象标签(公共)")
    @Tags({@org.apache.skywalking.apm.toolkit.trace.Tag(key = "参数", value = "arg[0]"),
            @org.apache.skywalking.apm.toolkit.trace.Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public UpdateObjectTagComRespDto updateObjectTagCom(UpdateObjectTagComReqDto reqDto) {


        //步骤0: 1-1-16查询标签详情 - queryTagDetail
        Tag tag = null;
        QueryTagDetailReq queryTagDetailReq = new QueryTagDetailReq();
        if (reqDto != null) {
            queryTagDetailReq.setTagName(reqDto.getTagName());//SimpleFieldAssign//sourceId:387852_1
            queryTagDetailReq.setBusinessSceneCode(reqDto.getBusinessSceneCode());//SimpleFieldAssign//sourceId:387865_1
            queryTagDetailReq.setSpaceId(reqDto.getSpaceId());//SimpleFieldAssign//sourceId:387853_1
        }

        /*1-1-16查询标签详情[203]   */
        Assert.isNull(queryTagDetailReq.getTagName(), "D1-1修改对象标签(公共)-1-1-16查询标签详情-标签名称不能为空", false);
        Assert.isNull(queryTagDetailReq.getBusinessSceneCode(), "D1-1修改对象标签(公共)-1-1-16查询标签详情-归属业务场景标识不能为空", false);
        Assert.isNull(queryTagDetailReq.getSpaceId(), "D1-1修改对象标签(公共)-1-1-16查询标签详情-创建于空间ID不能为空", false);
        tag = mTagService.queryTagDetail(queryTagDetailReq);


//步骤1: 1-1-16修改标签 - updateTag
        boolean bOOLEAN;
        if (tag != null) {
            Tag tag_2 = new Tag();
            if (tag != null) {
                tag_2.setTagId(tag.getTagId());//SimpleFieldAssign//sourceId:387856_1
            }
            if (reqDto != null) {
                tag_2.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:387859_1
            }

            /*1-1-16修改标签[110]   */
            Assert.isNull(tag_2.getTagId(), "D1-1修改对象标签(公共)-1-1-16修改标签-标签ID不能为空", false);
            bOOLEAN = mTagService.updateTag(tag_2);


        }

        UpdateObjectTagComRespDto retData = new UpdateObjectTagComRespDto();


        return retData;
    }
    //
}
